package com.shop.dao.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

import com.shop.dao.CategoryDao;
import com.shop.domain.Category;
import com.shop.util.MyBatisUtil;

/**
 * 
 * @author wenjuanhenxing
 * @version V1.0 CategoryDaoImpl.java 2016-2-10 下午11:41:26
 */
@Repository("categoryDaoImpl")
public class CategoryDaoImpl implements CategoryDao {

	@Override
	public boolean addCategory(Category category) {
		if (category == null) {
			return false;
		}
		SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
		int rowsAffected = sqlSession.insert(
				"com.shop.dao.mapping.CategoryMapper" + ".addCategory",
				category);
		MyBatisUtil.closeSession(sqlSession);
		return rowsAffected == 1;
	}

	@Override
	public boolean deleteCategoryById(Long id) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
		int rowsAffected = sqlSession.delete(
				"com.shop.dao.mapping.CategoryMapper" + ".deleteCategoryById",
				id);
		MyBatisUtil.closeSession(sqlSession);
		return rowsAffected == 1;
	}

	@Override
	public boolean deleteCategoryByName(String name) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
		int rowsAffected = sqlSession
				.delete("com.shop.dao.mapping.CategoryMapper"
						+ ".deleteCategoryByName", name);
		MyBatisUtil.closeSession(sqlSession);
		return rowsAffected == 1;
	}

	@Override
	public Long getIdByCategoryName(String name) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		Long ret = sqlSession.selectOne("com.shop.dao.mapping.CategoryMapper"
				+ ".getIdByCategoryName", name);
		MyBatisUtil.closeSession(sqlSession);
		return ret;
	}

	@Override
	public boolean updateCategoryName(Category category) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
		int rowsAffected = sqlSession.update(
				"com.shop.dao.mapping.CategoryMapper" + ".updateCategoryName",
				category);
		MyBatisUtil.closeSession(sqlSession);
		return rowsAffected == 1;
	}

	@Override
	public List<Category> getSubCategory(Long id) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		List<Category> ret = sqlSession.selectList(
				"com.shop.dao.mapping.CategoryMapper" + ".getSubCategory", id);
		MyBatisUtil.closeSession(sqlSession);
		return ret;
	}

	@Override
	public List<Category> getLargeCategory() {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		List<Category> ret = sqlSession
				.selectList("com.shop.dao.mapping.CategoryMapper"
						+ ".getLargeCategory");
		MyBatisUtil.closeSession(sqlSession);
		return ret;
	}

}
